import sys
import time
from selenium.webdriver.support import expected_conditions as ec
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait,Select
import openpyxl
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
class auto_plan:
    def __init__(self):
        self.options = Options()
        self.options.add_argument('--headless')
        self.options.add_argument('--disable-gpu')
        self.driver=webdriver.Chrome(options=self.options)
        self.plan=openpyxl.load_workbook(r'C:\testplan.xlsx')
        self.basic_sheet=self.plan['basic_info']
        self.testplan_sheet=self.plan['testplan']
        self.user=self.basic_sheet['B1'].value
        self.pwd = self.basic_sheet['B2'].value
        self.projectname=self.basic_sheet['B3'].value
        self.planname = self.basic_sheet['B4'].value
        self.starttime=self.basic_sheet['B5'].value
        self.endtime = self.basic_sheet['B6'].value
        self.os = self.basic_sheet['B7'].value
        self.product = self.basic_sheet['B8'].value
        self.config = self.basic_sheet['B9'].value
        self.plan_phase = self.basic_sheet['B10'].value
    def create_plan_clcik_new(self):#在在manager plan页面点击new新建plan
        print("正在登录TDMS")
        self.driver.get("http://tdms.lenovo.com/tdms/loginAction!login.do")
        self.driver.maximize_window()
        username_element = WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.ID, 'username')))  # 最长等待1000s
        username_element.send_keys(self.user)
        self.driver.find_element(By.XPATH, "//input[@id='password']").send_keys(self.pwd)
        self.driver.find_element(By.XPATH, "//input[@id='loginsub']").click()
        print("登录成功")
        print("正在进入manage test plan界面并点击new创建plan")
        element_test = WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.ID, 'menu_system_test')))
        element_test.click()
        element_manage_test_plan = WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.ID, 'test_menu_managetestplan')))
        element_manage_test_plan.click()
        element_project_name=WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.LINK_TEXT, self.projectname)))
        element_project_name.click()
        elment_new=WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.LINK_TEXT, 'New')))
        elment_new.click()#点完new就进入创建plan需要填咨询的页面
        print("Done!!!")
    def key_plan_info(self):#点完new后的页面填写咨询
        print("正在将excel test plan咨询对应填入到TDMS栏位")
        WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.ID, 'planDescription')))#等待Description :出现代表页面完全刷新出来了，因为Description :在页面最下面
        self.driver.find_element(By.ID,"planName").send_keys(self.planname)#填写planname
        self.driver.find_element(By.ID,"planVersion").send_keys('1')#填写version通常填1
        element_phase=self.driver.find_element(By.ID, "planInfoBO.planPhase") # 锁定phase下拉按钮select标签
        element_phase_1=Select(element_phase)#固定用法
        element_phase_1.select_by_value(self.plan_phase)#
        element_os = self.driver.find_element(By.ID, "os")  # 锁定os的select标签
        element_os_1 = Select(element_os)  # 固定用法
        element_os_1.select_by_value('474')  # 根据值来选择win11.474是win11
        self.driver.find_element(By.ID, "slanguage").send_keys('US')  # 填写language US
        element_product = self.driver.find_element(By.ID, "model")  # 锁定product的select标签
        element_product_1 = Select(element_product)  # 固定用法
        element_product_1.select_by_visible_text(self.product)#选好每一个config # 选择product
        config_list=self.config.split(',')#将字符串以逗号为区分转为列表
        element_sku = self.driver.find_element(By.ID, "sku")  # 锁定sku的select标签
        element_sku_1 = Select(element_sku)  # 固定用法
        for i in config_list:
            element_sku_1.select_by_visible_text(i)#选好每一个config
        element_start_time=self.driver.find_element(By.ID, "startDate")#锁定开始时间
        self.driver.execute_script("arguments[0].removeAttribute('readonly');", element_start_time)#使用JavaScript移除readonly属性
        element_start_time.send_keys(self.starttime)
        element_end_time = self.driver.find_element(By.ID, "endDate")  # 锁定开始时间
        self.driver.execute_script("arguments[0].removeAttribute('readonly');", element_end_time)  # 使用JavaScript移除readonly属性
        element_end_time.send_keys(self.endtime)
        self.driver.find_element(By.XPATH, "//*[@id='planCreateForm']/table/tbody/tr/td/table/tbody/tr[11]/td[1]/input").click() # 点击save
        print("Done!!!")
    def add_case(self):#进入add case页面
        print("正在依次将test case拉入到TDMS plan")
        all_rows = self.testplan_sheet.max_row#获取最大行数
        for y in range(1, all_rows + 1):
            try:
                element_add = WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.LINK_TEXT, 'Add')))#等待add出现
                element_add_1=WebDriverWait(self.driver, 1000).until(ec.element_to_be_clickable((By.LINK_TEXT, 'Add')))#等待元素可点击，不知道这个能不能解决add有时候还不能点，因为点save后有时候窗口消失的慢，挡住元素
                element_add_1.click()
                element_caseid = WebDriverWait(self.driver, 1000).until(ec.presence_of_element_located((By.XPATH, "//*[@id='head']/table/tbody/tr[2]/td[2]/input")))  # 等等CASEID 输入框出现
                element_caseid.send_keys(self.testplan_sheet[f'A{y}'].value)#依次把A1A2A3的值传入到输入框
                self.driver.find_element(By.XPATH,"//*[@id='head']/table/tbody/tr[6]/td/input").click()#点search
                checkbox=WebDriverWait(self.driver, 60).until(ec.presence_of_element_located((By.XPATH, "/html/body/div[*]/div[2]/form[2]/div[2]/table/tbody/tr[1]/td[1]/input[1]")))#等60秒，如果有出现checkbox就代表搜索到case,否则就是没拉到
                checkbox.click()
                self.driver.find_element(By.ID, "save").click()#点save
                target_cell = self.testplan_sheet.cell(column=2, row=y)
                target_cell.value = "Done"
                self.plan.save(r'C:\testplan.xlsx')
                time.sleep(3)
                print(self.testplan_sheet[f'A{y}'].value+'拉取成功')
            except:
                target_cell = self.testplan_sheet.cell(column=2, row=y)
                target_cell.value = "60秒后依然无法搜索到此case"
                self.plan.save(r'C:\testplan.xlsx')
        print("Done!!!")
    def set_matrix(self):#设置matrix到win11，拉出来的case回填的时候就只有win11
        print("正在设置所有case的matrix为Win11_X64")
        time.sleep(2)
        self.driver.find_element(By.ID, "tableId_checkAll").click()  # 点case id前面的checkbox. 勾选全部
        time.sleep(2)
        self.driver.find_element(By.ID, "matric-tip").click()# 点matrix
        popup_windows = self.driver.window_handles[1]
        self.driver.switch_to.window(popup_windows)
        add_Matrix_input=WebDriverWait(self.driver, 60).until(ec.presence_of_element_located((By.XPATH,"/html/body/form/div/table[1]/tbody/tr/td/table/tbody/tr/td[1]/input")))
        add_Matrix_input.send_keys('win11_x64')
        self.driver.find_element(By.ID, "addBtn").click()  # 点add
        self.driver.find_element(By.ID, "okBtn").click()  # 点ok
        time.sleep(1)
        self.driver.switch_to.window(self.driver.window_handles[0])
        self.driver.find_element(By.XPATH, "//*[@id='planCaselistForm']/table[3]/tbody/tr/td/input[2]").click()  # 点FINISH
        time.sleep(3)
        print("Done!!!")
if __name__ == "__main__":
    case_go = auto_plan()
    case_go.create_plan_clcik_new()#这里不会报错除非TDMS页面改动这是点new之前
    try:
        case_go.key_plan_info()
    except:
        print("请检查excel咨询是否填错")
        time.sleep(27)
        sys.exit(1)#错误代码1退出终止整个程序，后面的代码也不执行了
    case_go.add_case()
    case_go.set_matrix()
    print("全部完成，快去check吧！！！！！")
    time.sleep(30)













